package com.itheima;

import com.itheima.dao.IUserDao;
import com.itheima.model.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.apache.ibatis.io.Resources;

import javax.jws.soap.SOAPBinding;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisApplication {
    private SqlSession sqlSession;
    @Before
    public  void  openSession() throws IOException{
        InputStream in = Resources.getResourceAsStream("mybatisConlig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(in);
        sqlSession = sqlSessionFactory.openSession();
    }
    @After
    public void closeSession(){
        if(sqlSession!=null){
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Test
    public void  test(){
        System.out.println("hell");
    }
    @Test
    public void testUser(){
        User user =new User();
        user.setId(1);
        user.setName("lily");
        user.setSex("女");
        System.out.println(user);
    }
    @Test
    public void testMybatis() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatisConlig.xml");
        SqlSessionFactoryBuilder  sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory =sqlSessionFactoryBuilder.build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession();
        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        List<User> userList =userDao.findAll();
        for(User user:userList){
            System.out.println(user);
        }

    }
    @Test
    public void testMybatis2() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatisConlig.xml");
        SqlSessionFactoryBuilder  sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory =sqlSessionFactoryBuilder.build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession();
        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        User user = userDao.findOneById(1);

            System.out.println(user);


    }
    @Test
    public void testMybatis3() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatisConlig.xml");
        SqlSessionFactoryBuilder  sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory =sqlSessionFactoryBuilder.build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession();
        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        List<User> userList =userDao.findByName("lily");
        for(User user:userList){
            System.out.println(user);
        }

    }
    @Test
    public void testMybatis4() throws IOException {

        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        int res = userDao.save("lucy","女");
        System.out.println(res);


    }
    @Test
    public void testMybatis5() throws IOException {

        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        User user =new User();
        user.setSex("Man");
        user.setName("wss");
        int res = userDao.save2(user);
        System.out.println(user);


    }
    @Test
    public void testMybatis6() throws IOException {

        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        int res = userDao.update("wsss","nan",2);


        System.out.println(res);


    }
    @Test
    public void testMybatis7() throws IOException {

        IUserDao userDao =sqlSession.getMapper(IUserDao.class);
        User user =new User();
        user.setSex("Man");
        user.setName("wss");
        int res = userDao.save2(user);
        System.out.println(user);


    }

}
